/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.entity;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author rcrida
 */
@Entity
@NamedQuery(name="findTransitionBySourceStateAndTaskStateMachine",
            query="SELECT t FROM Transition t WHERE t.sourceState = :sourceState AND t.taskStateMachine = :taskStateMachine")
public class Transition implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private boolean ownerResponsible;
    
    private boolean userResponsible;

    @ManyToOne
    private TaskStateMachine taskStateMachine;

    @ManyToOne
    private Status status;

    @ManyToOne
    private State sourceState;

    @ManyToOne
    private State destinationState;

    @ManyToMany
    private Collection<Role> responsibleRoles;

    protected Transition() {
    }

    public Transition(boolean ownerResponsible, boolean userResponsible) {
        this.ownerResponsible = ownerResponsible;
        this.userResponsible = userResponsible;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setDestinationState(State destinationState) {
        this.destinationState = destinationState;
    }

    public void setOwnerResponsible(boolean ownerResponsible) {
        this.ownerResponsible = ownerResponsible;
    }

    public Collection<Role> getResponsibleRoles() {
        return responsibleRoles;
    }

    public void setResponsibleRoles(Collection<Role> responsibleRoles) {
        this.responsibleRoles = responsibleRoles;
    }

    public State getDestinationState() {
        return destinationState;
    }

    public State getSourceState() {
        return sourceState;
    }

    public boolean isUserResponsible() {
        return userResponsible;
    }

    public boolean isOwnerResponsible() {
        return ownerResponsible;
    }

    public Status getStatus() {
        return status;
    }

    public void setSourceState(State sourceState) {
        this.sourceState = sourceState;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public void setUserResponsible(boolean userResponsible) {
        this.userResponsible = userResponsible;
    }

    public TaskStateMachine getTaskStateMachine() {
        return taskStateMachine;
    }

    public void setTaskStateMachine(TaskStateMachine taskStateMachine) {
        this.taskStateMachine = taskStateMachine;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Transition)) {
            return false;
        }
        Transition other = (Transition) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        if ((this.destinationState == null && other.destinationState != null) || (this.destinationState != null && !this.destinationState.equals(other.destinationState))) {
            return false;
        }
        if (this.ownerResponsible != other.ownerResponsible) {
            return false;
        }
        if ((this.responsibleRoles == null && other.responsibleRoles != null) || (this.responsibleRoles != null && !this.responsibleRoles.equals(other.responsibleRoles))) {
            return false;
        }
        if ((this.sourceState == null && other.sourceState != null) || (this.sourceState != null && !this.sourceState.equals(other.sourceState))) {
            return false;
        }
        if ((this.status == null && other.status != null) || (this.status != null && !this.status.equals(other.status))) {
            return false;
        }
        if ((this.taskStateMachine == null && other.taskStateMachine != null) || (this.taskStateMachine != null && !this.taskStateMachine.equals(other.taskStateMachine))) {
            return false;
        }
        if (this.userResponsible != other.userResponsible)
            return false;
        return true;
    }

    @Override
    public String toString() {
        StringBuffer strBuffer = new StringBuffer();
        strBuffer.append(getSourceState().getName());
        strBuffer.append(" -> ");
        strBuffer.append(getDestinationState().getName());
        strBuffer.append(" (");
        strBuffer.append(getStatus().getName());
        strBuffer.append(", Roles = [");
        strBuffer.append(StringUtils.join(responsibleRoles, ", "));
        strBuffer.append("])");
        if (isOwnerResponsible())
            strBuffer.append(" + Owner");
        if (isUserResponsible())
            strBuffer.append(" + User");
        return strBuffer.toString();
    }

}
